<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  

  
  <title>Hexo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta property="og:type" content="website">
<meta property="og:title" content="Hexo">
<meta property="og:url" content="http://cui-rongpei.gitee.io/blog/page/2/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:locale" content="en_US">
<meta property="article:author" content="John Doe">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/blog/atom.xml" title="Hexo" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  
<link rel="stylesheet" href="/blog/css/style.css">

<meta name="generator" content="Hexo 5.4.2"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/blog/" id="logo">Hexo</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/blog/">Home</a>
        
          <a class="main-nav-link" href="/blog/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/blog/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://cui-rongpei.gitee.io/blog"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main">
  
    <article id="post-C++" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/blog/2022/09/11/C++/" class="article-date">
  <time datetime="2022-09-11T06:45:25.642Z" itemprop="datePublished">2022-09-11</time>
</a>
    
  <div class="article-category">
    <a class="article-category-link" href="/blog/categories/C-C/">C/C++</a>
  </div>

  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/blog/2022/09/11/C++/">C/C++</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h1 id="C-核心编程"><a href="#C-核心编程" class="headerlink" title="C++核心编程"></a>C++核心编程</h1><h2 id="内存分区模型"><a href="#内存分区模型" class="headerlink" title="内存分区模型"></a>内存分区模型</h2><ul>
<li><p>代码区：存放函数体的二进制代码，由操作系统进行管理</p>
</li>
<li><p>全局区：存放全局变量、静态变量和常量</p>
</li>
<li><p>栈区：由编译器自动分配释放，存放函数的参数值，局部变量等（<strong>注意不要返回局部变量的地址</strong>）</p>
</li>
<li><p>堆区：由程序员分配和释放，若程序员不释放，程序结束时由操作系统释放</p>
<p>内存四区的意义：</p>
<p>​    不同区域存放的数据，赋予不同的生命周期，给我们更大的灵活编程</p>
<p><strong>堆区</strong></p>
</li>
</ul>
<p>在C++中主要利用new在堆区开辟内存，释放用操作符delete</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">int * func()</span><br><span class="line">&#123;//利用new关键字，可以将数据开辟到堆区</span><br><span class="line">	int * a = new int(10);//new开辟一个空间，里面放着10，返回的地址用指针a接收</span><br><span class="line">	return a;</span><br><span class="line">&#125;</span><br><span class="line">int main()</span><br><span class="line">&#123;</span><br><span class="line">	int *p = func();</span><br><span class="line">	cout &lt;&lt; *p &lt;&lt; endl;</span><br><span class="line">	return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="引用"><a href="#引用" class="headerlink" title="引用"></a>引用</h2><p><strong>引用注意事项</strong>：</p>
<ul>
<li><p>引用必须初始化</p>
</li>
<li><p>引用在初始化后不可再改变</p>
<p><strong>引用的本质</strong></p>
</li>
</ul>
<p>引用的本质在C++内部实现是一个指针常量</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">int &amp;ref = a;//自动转换为int * const ref = &amp;a,这也说明为什么引用不可更改</span><br></pre></td></tr></table></figure>

<p><strong>常量引用</strong></p>
<p>​    作用：主要用来修饰形参，防止误操作</p>
<p>错误示例：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">int &amp; ref = 10;//错误，引用必须引一块合法的内存空间，比如堆栈上的</span><br></pre></td></tr></table></figure>

<p>加上const之后，编译器将代码修改 int temp = 10; const int &amp; ref  = temp;</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">const int &amp; ref = 10;//正确</span><br></pre></td></tr></table></figure>

<p><strong>在函数形参列表中，可以加const修饰形参，防止形参改变实参</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">void func(const int &amp;a)</span><br></pre></td></tr></table></figure>

<h2 id="函数提高"><a href="#函数提高" class="headerlink" title="函数提高"></a>函数提高</h2><h3 id="函数的默认参数"><a href="#函数的默认参数" class="headerlink" title="函数的默认参数"></a>函数的默认参数</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">int func(int a, int b = 20, int c = 30)&#123;</span><br><span class="line">	return a+b+c;</span><br><span class="line">&#125;</span><br><span class="line">int main()&#123;</span><br><span class="line">	cout &lt;&lt; func(10) &lt;&lt; endl;//输出60</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>注意事项：1、如果某个位置有默认参数，那么从这个位置往后，从左到右都必须有默认值</p>
<p>​    2、如果函数声明时有默认参数，那么函数实现时不能有默认参数。</p>
<h3 id="函数占位参数"><a href="#函数占位参数" class="headerlink" title="函数占位参数"></a>函数占位参数</h3><p>C++中函数的形参列表中可以有占位参数，用来做占位，调用函数时必须填补该位置</p>
<h3 id="函数重载"><a href="#函数重载" class="headerlink" title="函数重载"></a>函数重载</h3><p>作用：函数名可以相同，提高复用性</p>
<p>注意事项：函数的返回值不可以作为函数重载的条件</p>
<p><strong>函数重载满足条件</strong></p>
<ul>
<li>同一个作用域下</li>
<li>函数名称相同</li>
<li>函数<strong>参数类型不同</strong> 或者 <strong>个数不同</strong> 或者 <strong>顺序不同</strong></li>
</ul>
<h3 id="函数重载注意事项"><a href="#函数重载注意事项" class="headerlink" title="函数重载注意事项"></a>函数重载注意事项</h3><ul>
<li>引用作为重载的条件</li>
<li>函数重载遇到函数默认参数</li>
</ul>
<p>1、引用作为重载的条件</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">void func(int &amp;a)&#123;</span><br><span class="line">	cout &lt;&lt; &quot;func(int &amp;a)调用&quot; &lt;&lt; endl;</span><br><span class="line">&#125;</span><br><span class="line">void func(const int &amp;a)&#123;</span><br><span class="line">	cout &lt;&lt; &quot;func(const int &amp;a)调用&quot; &lt;&lt; endl;</span><br><span class="line">&#125;</span><br><span class="line">int main()&#123;</span><br><span class="line">	int a = 10;</span><br><span class="line">	func(a);//调用func(int &amp;a)</span><br><span class="line">	</span><br><span class="line">	func(10);//调用func(const int &amp;a)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>2、函数重载碰到默认参数(坑)</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">void func2(int a)&#123;</span><br><span class="line">	cout &lt;&lt; &quot;func2(int a)调用&quot; &lt;&lt; endl;</span><br><span class="line">&#125;</span><br><span class="line">void func2(int a， int b = 10)&#123;</span><br><span class="line">	cout &lt;&lt; &quot;func2(int a,int b=10)调用&quot; &lt;&lt; endl;</span><br><span class="line">&#125;</span><br><span class="line">int main()&#123;</span><br><span class="line">	int a=10;</span><br><span class="line">	func(a);//当函数重载碰到默认参数，出现二义性报错，应尽量避免这种情况</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="类和对象"><a href="#类和对象" class="headerlink" title="类和对象"></a>类和对象</h2><p>C++面向对象的三大特性：封装、继承、多态</p>
<p>万事万物皆为对象，对象上有其属性和行为</p>
<h3 id="封装"><a href="#封装" class="headerlink" title="封装"></a>封装</h3><p>封装的意义：</p>
<ul>
<li>将属性和行为作为一个整体，表现生活中的事物</li>
<li>将属性和行为加以权限控制</li>
</ul>
<p>封装意义一：</p>
<p>​    在设计类的时候，属性和行为写在一起，表现事物</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">class Circle&#123;</span><br><span class="line">//访问权限</span><br><span class="line">public:</span><br><span class="line">	//属性</span><br><span class="line">	int m_R; //半径</span><br><span class="line">	//行为</span><br><span class="line">	double calculateZC()&#123;</span><br><span class="line">		return 2*3.1415926*m_R;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;;</span><br><span class="line">int main()&#123;</span><br><span class="line">	Circle c1;//通过圆类，创建具体的圆（对象）</span><br><span class="line">	c1.m_R = 10;//给属性赋值</span><br><span class="line">	cout &lt;&lt; &quot;圆的周长为：&quot; &lt;&lt; c1.calculateZC() &lt;&lt;endl;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>封装意义二：</p>
<p>访问权限有三种</p>
<p>1、public    公共权限    类内可以访问，类外可以访问</p>
<p>2、protected 保护权限    类内可以访问，类外不可以访问    继承类也可以访问父类的保护的的内容</p>
<p>3、private    私有权限    类内可以访问，类外不可以访问    继承类不可以访问父类的私有的内容</p>
<p><strong>class和struct的区别</strong></p>
<p>唯一区别在于<strong>默认的访问权限</strong>不同</p>
<ul>
<li>struct 默认权限为公共</li>
<li>class 默认权限为私有</li>
</ul>
<h4 id="成员属性设置为私有"><a href="#成员属性设置为私有" class="headerlink" title="成员属性设置为私有"></a>成员属性设置为私有</h4><p>1、可以自己控制读写权限</p>
<p>2、对于写数据可以检测数据的有效性</p>
<h3 id="对象的初始化和清理"><a href="#对象的初始化和清理" class="headerlink" title="对象的初始化和清理"></a>对象的初始化和清理</h3><h4 id="构造函数和析构函数"><a href="#构造函数和析构函数" class="headerlink" title="构造函数和析构函数"></a>构造函数和析构函数</h4><p><strong>对象的初始化和清理</strong></p>
<p>构造函数语法：类名（）{}</p>
<p>1、构造函数，没有返回值也不写void</p>
<p>2、函数名称与类名相同</p>
<p>3、构造函数可以有参数，因此可以重载</p>
<p>4、程序在调用对象时候会自动调用构造，无需手动调用，而且只调用一次</p>
<p>析构函数语法：~类名（）{}</p>
<p>1、构造函数，没有返回值也不写void</p>
<p>2、函数名称与类名相同，在名称前加~</p>
<p>3、不可以有参数，不可以重载</p>
<p>4、程序在对象销毁前会自动调用析构，无需手动调用，而且只会调用一次</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">int LocateElem(SqList L, ElemType e, Status(*compare)(ElemType, ElemType))</span><br><span class="line">&#123; //compare（）是数据元素判定函数，满足为1，否则为0</span><br><span class="line">  // 返回L中第一个与e满足关系compare的数据元素的位序。若不存在，则返回0</span><br><span class="line">  int i=1; </span><br><span class="line">  ElemType *p=L.elem; </span><br><span class="line">  while(i&lt;=L.length &amp;&amp; !compare(*p++, e))</span><br><span class="line">    i++; </span><br><span class="line">  if(i&lt;=L.length) </span><br><span class="line">    return i; </span><br><span class="line">  else </span><br><span class="line">    return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>LocateElem（）函数也要调用一类函数compare()。由LocateElem（）函数的声明可以看出：要求这类函数有两个形参，其类型均为ElemType；函数的返回值为Status类型。不仅如此，根据LocateElem（）函数的说明，当compare（）函数的两个形参满足给定条件时。返回值为1，否则为0.只有满足这种条件的函数才能作为替代compare（）函数的实参函数。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">void ListTraverse(SqList L, void(*visit)(ElemType&amp;))</span><br><span class="line">&#123; //依次对L的每个数据元素调用函数visit()</span><br><span class="line"></span><br><span class="line">  ElemType *p=L.elem; </span><br><span class="line">  for(int i=1; i&lt;=L.length; i++) </span><br><span class="line">    visit(*p++); </span><br><span class="line">  printf(&quot;\n&quot;);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>基本操作函数ListTraverse（）还要调用visit()函数，并将visit()函数设为ListTraverse的形参。把函数</p>
<p>visit()作为形参的原因，是要在ListTraverse（）中根据情况调用不同的函数而不是一个固定的函数。从作为形参的visit()函数得知，满足哪些条件的函数可以被ListTraverse（）函数嗲用。在函数类形参的声明中指定了visit()的函数类型，也就是函数返回值的类型（void）。在声明中还指定了<strong>visit（）函数形参的个数（一个）</strong>，和<strong>类型（ElemType的引用类型）</strong>。所有满足条件的函数（**返回值为void类型，有一个形参，且类型为ElemType&amp;**），都可以作为ListTraverse（）函数的实参。</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://cui-rongpei.gitee.io/blog/2022/09/11/C++/" data-id="cl8h5o78h0001sot9be0rbmd0" class="article-share-link">Share</a>
      
      
    </footer>
  </div>
  
</article>


  


  <nav id="page-nav">
    
    <a class="extend prev" rel="prev" href="/blog/">&amp;laquo; Prev</a><a class="page-number" href="/blog/">1</a><span class="page-number current">2</span>
  </nav>

</section>
        
          <aside id="sidebar">
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Categories</h3>
    <div class="widget">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/blog/categories/C-C/">C/C++</a></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0/">深度学习</a><ul class="category-list-child"><li class="category-list-item"><a class="category-list-link" href="/blog/categories/%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0/%E4%B8%9C%E6%96%B9%E8%80%80%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0/">东方耀机器学习</a></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0/%E4%BD%BF%E7%94%A8C-%E7%BC%96%E5%86%99%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0%E6%A1%86%E6%9E%B6/">使用C++编写深度学习框架</a></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0/%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0%E7%8E%AF%E5%A2%83%E6%90%AD%E5%BB%BA/">深度学习环境搭建</a></li></ul></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/%E8%AE%BA%E6%96%87%E7%AC%94%E8%AE%B0/">论文笔记</a></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/%E8%BF%91%E6%9C%9F%E8%A7%84%E5%88%92/">近期规划</a></li></ul>
    </div>
  </div>


  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2022/09/">September 2022</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/blog/2022/09/25/%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0/%E4%BD%BF%E7%94%A8C++%E7%BC%96%E5%86%99%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0%E6%A1%86%E6%9E%B6/gflags%E5%85%A5%E9%97%A8/">gflags入门</a>
          </li>
        
          <li>
            <a href="/blog/2022/09/25/%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0/%E4%BD%BF%E7%94%A8C++%E7%BC%96%E5%86%99%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0%E6%A1%86%E6%9E%B6/glog%E5%85%A5%E9%97%A8/">glog入门</a>
          </li>
        
          <li>
            <a href="/blog/2022/09/25/%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0/%E4%BD%BF%E7%94%A8C++%E7%BC%96%E5%86%99%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0%E6%A1%86%E6%9E%B6/bazel%E5%85%A5%E9%97%A8/">bazel入门</a>
          </li>
        
          <li>
            <a href="/blog/2022/09/24/%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0/%E4%BD%BF%E7%94%A8C++%E7%BC%96%E5%86%99%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0%E6%A1%86%E6%9E%B6/%E4%BD%BF%E7%94%A8C++%E7%BC%96%E5%86%99%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0%E6%A1%86%E6%9E%B6/">前言 环境准备</a>
          </li>
        
          <li>
            <a href="/blog/2022/09/24/%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0/%E7%8E%AF%E5%A2%83%E6%90%AD%E5%BB%BA/%E8%B6%85%E7%AE%97%E5%B9%B3%E5%8F%B0%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0%E7%8E%AF%E5%A2%83%E6%90%AD%E5%BB%BA/">超算平台深度学习环境搭建</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2022 John Doe<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/blog/" class="mobile-nav-link">Home</a>
  
    <a href="/blog/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


  
<link rel="stylesheet" href="/blog/fancybox/jquery.fancybox.css">

  
<script src="/blog/fancybox/jquery.fancybox.pack.js"></script>




<script src="/blog/js/script.js"></script>




  </div>
</body>
</html>